#include "touch.h"

TOUCH::TOUCH()
{
    X = 0;
    Y = 0;
    fp = fopen("/dev/input/event0","r+");
}
TOUCH::~TOUCH()
{
    fclose(fp);
    cout << "触摸屏的析构函数成功调用" << endl;
}
/* 1:右 2:左 3:上 4:下 */
int TOUCH::fx()
{
    bool xnode = false;
    bool ynode = false;
    int x=0,y=0,x1=0,y1=0,n=0;
    
    while(1)
    {
        bzero(&touchscreen,sizeof(touchscreen));
        fread(&touchscreen,1,sizeof(touchscreen),fp);
        if(touchscreen.type == EV_ABS)
        {
            if(touchscreen.code == ABS_X && x1 == 0)
            x1 = touchscreen.value;

            if(touchscreen.code == ABS_Y && y1 == 0)
            y1 = touchscreen.value;

            if(touchscreen.code == ABS_X && x1!=0)
            {
            x = touchscreen.value;
            xnode = true;
            }
            if(touchscreen.code == ABS_Y && y1!=0)
            {
            y = touchscreen.value;
            ynode = true;
            }           
            if(xnode && ynode)
            {
                if((x-x1)>100 && abs((y-y1))<50 && n==0)
                {
                    printf("右划\n");
                    n = 1;
                    return 1;
                }
                else if((x1-x)>100 && abs((y-y1))<50 && n==0)
                {
                    printf("左划\n");
                    n = 1;
                    return 2;                   
                }
                else if(abs(x-x1)<50 && (y1-y)>80 && n==0)
                {
                    printf("上划\n");
                    n = 1;
                    return 3;                  
                }
                else if(abs(x-x1)<50 && (y-y1)>80 && n==0)
                {
                    printf("下划\n");
                    n = 1;
                    return 4;                   
                }
                xnode = false;
                ynode = false;
            }  
        }
        if(touchscreen.type == EV_KEY)
        {
            if(touchscreen.value == 0)
            {
            x1 = 0;
            y1 = 0;
             n = 0;
            }
        }
    }
}

/*获取坐标*/
void TOUCH::coordinates()
{
    bool xnode = false;
    bool ynode = false;
    int x,y;
    float c = 800,d = 1080;
    while(1)
    {
        bzero(&touchscreen,sizeof(touchscreen));
        fread(&touchscreen,1,sizeof(touchscreen),fp);
        if(touchscreen.type == EV_ABS)
        {
            if(touchscreen.code == ABS_X)
            {
                x = touchscreen.value;
                xnode = true;
            }
            if(touchscreen.code == ABS_Y)
            {
                y = touchscreen.value;
                ynode = true;
            }
            if(xnode && ynode)
            {
                X = x*(c/d);
                //cout << c/d << endl;
                Y = y*(c/d);
                return;
            }
        }
    }
}

int TOUCH::showinfoX()
{
    return X;
}
int TOUCH::showinfoY()
{
    return Y;
}
void TOUCH::info()
{
    cout << "(X:" << X << "Y:" << Y << ")" << endl;
}

/*返回区块默认4*4*/
int TOUCH::XYinfo()
{
    coordinates();
    if(Y>0 && Y<120)
    {
        if(X>0 && X<200)
            return 1;
        else if(X>200 && X<400)
            return 2;
        else if(X>400 && X<600)
            return 3;
        else if(X>600 && X<800)
            return 4;
    }
    else if(Y>120 && Y<240)
    {
        if(X>0 && X<200)
            return 5;
        else if(X>200 && X<400)
            return 6;
        else if(X>400 && X<600)
            return 7;
        else if(X>600 && X<800)
            return 8;    
    }
    else if(Y>240 && Y<360)
    {
        if(X>0 && X<200)
            return 9;
        else if(X>200 && X<400)
            return 10;
        else if(X>400 && X<600)
            return 11;
        else if(X>600 && X<800)
            return 12;    
    }
    else if(Y>360 && Y<480)
    {
        if(X>0 && X<200)
            return 13;
        else if(X>200 && X<400)
            return 14;
        else if(X>400 && X<600)
            return 15;
        else if(X>600 && X<800)
            return 16;    
    }
}

/* 返回区块通用a*a */
int TOUCH::XYinfo(int a)
{
    coordinates();
    int b = 0;
    for(int i=0;i<a;i++)
    {
        if(Y>((480/a)*i) && Y<((480/a)*(i+1)))
        {
            for(int j=0;j<a;j++)
            {
                b = j + 1 + a * i;
                if(X>((800/a)*j) && X<((800/a)*(j+1)))
                {
                    return b;
                }
            }
        }
    }
    return 0;
}